<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <!-- <el-card>
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入订单编号" v-model="queryInfo.query" clearable @clear="getOrderList">
            <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
          </el-input>
        </el-col>
    </el-row>-->

    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="5">
          <el-input placeholder="请输入订单编号" v-model="queryInfo.query" clearable @clear="getOrderList">
            <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="3">
          <el-select v-model="value" @change="handleChange(value)" clearable placeholder="请选择">
            <el-option
              v-for="item in status"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-col>
        <el-col :span="2">
          <el-button type="warning" @click="deliver(2)">发货</el-button>
        </el-col>
        <el-col :span="2">
          <el-button type="danger" @click="deliver(5)">退款</el-button>
        </el-col>
        <el-col :span="2">
          <el-button type="success" @click="excel">导出订单</el-button>
        </el-col>
      </el-row>

      <!-- 订单列表数据 -->
      <el-table :data="orderlist" border stripe @selection-change="selsChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="订单编号" prop="out_trade_no"></el-table-column>
        <el-table-column label="订单价格(元)" prop="order_total_price" width="180px"></el-table-column>

        <el-table-column label="订单状态" prop="status" width="180px">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.status == 0 ">待付款</el-tag>
            <el-tag
              type="primary"
              v-else-if="scope.row.status == 1 "
              @click="deliver(scope.row.id)"
            >待发货</el-tag>
            <el-tag type="success" v-else-if="scope.row.status == 2 ">待收货</el-tag>
            <el-tag type="warning" v-else-if="scope.row.status == 4 ">待退款</el-tag>
            <el-tag type="warning" v-else-if="scope.row.status == 5 ">已退款</el-tag>
            <el-tag type="warning" v-else>完成</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="下单时间" prop="create_time" width="220px">
          <template slot-scope="scope">{{scope.row.create_time|dateFormat}}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="showBox"></el-button>
            <el-button type="success" size="mini" icon="el-icon-location"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1,2,5,10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      ></el-pagination>
    </el-card>

    <!-- 修改地址对话框 -->
    <el-dialog title="修改地址" :visible.sync="addressVisible" width="50%" @close="addressClosed">
      <el-form
        :rules="addressFormRules"
        :model="addressForm"
        ref="addressFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader :options="cityData" v-model="addressForm.address1"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressVisible = false">取 消</el-button>
        <el-button type="primary" @click="addressVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import cityData from './citydata.js'
import { server } from '../api/index'
export default {
  data() {
    return {
      sels: [], //选中的值显示
      //获取列表参数对象
      queryInfo: {
        query: '',
        //当前页数
        pagenum: 1,
        //当前页显示多少条
        pagesize: 10,
        status: 0
      },
      total: 0,
      orderlist: [],
      addressVisible: false,
      addressForm: {
        address1: [],
        address2: ''
      },
      addressFormRules: {
        address1: [
          { required: true, message: '请选择省市区县', trigger: 'blur' }
        ],
        address2: [
          {
            required: true,
            message: '请填写详细地址',
            trigger: 'blur'
          }
        ]
      },
      cityData,
      status: [
        {
          value: '-1',
          label: '待付款'
        },
        {
          value: '1',
          label: '待发货'
        },
        {
          value: '2',
          label: '待收货'
        },
        {
          value: '4',
          label: '待退款'
        },
        {
          value: '6',
          label: '已完成'
        }
      ],
      value: ''
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    async getOrderList() {
      const { data: res } = await server.getOrderList(this.queryInfo)
      if (res.code !== 200) {
        return this.$message.error('获取订单列表失败')
      }
      this.total = res.data.total
      this.orderlist = res.data.data
    },
    handleChange() {
      this.queryInfo.status = this.value
      this.getOrderList()
    },
    selsChange(sels) {
      this.sels = sels
    },
    deliver(status) {
      var ids = this.sels.map(item => item.id).join() //获取所有选中行的id组成的字符串，以逗号分隔
      this.$confirm('是否执行此操作, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.doDeliver(ids, status)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    async doDeliver(ids, status) {
      const { data: res } = await server.updateStatus(ids, status)
      if (res.code !== 200) {
        return this.$message.error('请选择正确的订单状态')
      } else {
        this.$message.success('执行操作成功')
        this.getOrderList()
      }
    },
    //监听分页改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    //监听页码值
    handleCurrentChange(newPage) {
      // console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    showBox() {
      this.addressVisible = true
    },
    addressClosed() {
      this.$refs.addressFormRef.resetFields()
    },

    async excel() {
      var status = this.queryInfo.status
      const { data: res } = await server.getExcel(status)
      window.open('http://shop.cn/api/v1.Order/exportOrders/' + status)
    }
  }
}
</script>
<style scoped>
.el-cascader {
  width: 100%;
}
</style>